# Sock编程
import socket

# 创建套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 连接服务器
sock.connect(('www.baidu.com', 80))

# 发送数据
sock.send(b'GET / HTTP/1.1\r\nHost: www.baidu.com\r\n\r\n')

# 接收数据
data = b''
while True:
    chunk = sock.recv(1024)
    if not chunk:
        break
    data += chunk
    print(chunk.decode("utf-8")) # 解码成字符串
# 关闭套接字 
sock.close()